<?php
Class ExcelAction extends CommonAction{

    //
    public function index()
    {

        $this->display();
    }

    //上传
    public  function upload(){
        $name_s=$_POST['subjects'];
        import('ORG.Net.UploadFile');
        $upload=new UploadFile();


        $upload->maxSize=1024*1024*500;   // 上传文件的最大值
        $upload->allowExts=array('xls','xlsx');    // 允许上传的文件后缀 留空不作后缀检查
        $upload->autoSub=true;// 启用子目录保存文件
        $upload->subType = 'date';
        $upload->dateFormat='Ymd';//时间格式
        $upload->savePath='./uploads/';   // 上传文件保存路径



        if (!$upload->upload()) {
            //如果上传失败以下
            echo("<script>alert('不能上传过大的照片')</script>");
            $this->error();
        }else{
            $info=$upload->getUploadFileInfo();
            $exts=$info[0]['extension'];
            $file_url=$info[0]['savepath'].$info[0]['savename'];
            $this->read_exsl($file_url,$exts,$name_s);
        }


    }

    public function read_exsl($file_url,$exts,$name_s){
       import('ORG.Util.PHPExcel');
        $php_excel=new PHPExcel();

        if($exts=='xls'){
            import('ORG.Util.PHPExcel.Reader.Excel5');
            $php_reader=new PHPExcel_Reader_Excel5();
        }else if($exts=='xlsx'){
            import('ORG.Util.PHPExcel.Reader.Excel2007');
            $php_reader=new PHPExcel_Reader_Excel2007();
        }

        $PHPExcel=$php_reader->load($file_url);

        $current_sheet=$PHPExcel->getSheet(0);
        $all_colmun=$current_sheet->getHighestColumn();
        $all_row=$current_sheet->getHighestRow();
        for($currentRow=1;$currentRow<=$all_row;$currentRow++){
            for($currentColmun='A';$currentColmun<=$all_colmun;$currentColmun++){
                $address=$currentColmun.$currentRow;
                $data[$currentRow][$currentColmun]=$current_sheet->getCell($address)->getValue();
            }
        }
        $this->save_import($data);
    }

    public function save_import($data){

//        foreach($data as $k=>$v){
//            if($k>=2){
//                $dat=array(
//                    'student_number'=>$v['B'],
//                    'test_number'=>$v['C'],
//                    'class'=>$v['D'],
//                    'student_name'=>$v['E'],
//                    'math_num'=>$v['F'],
//                    'language_num'=>$v['G'],
//                    'chines_num'=>$v['H'],
//                    'politics_num'=>$v['I'],
//                    'history_num'=>$v['J'],
//                    'geography_num'=>$v['K'],
//                    'biology_num'=>$v['L'],
//                    'chemistry_num'=>$v['M'],
//                    'physics_num'=>$v['N'],
//                    'eligible_subjects'=>$v['O'],
//                    'total_points'=>$v['P'],
//                    'average'=>$v['Q'],
//                    'class_level'=>$v['R'],
//                    'school_level'=>$v['S'],
//                );
//            }
//            M('result_db')->add($dat);
//                p($dat);
//        }

                foreach($data as $k=>$v){
                $dat[]=array(
                    'student_number'=>$v['B'],
                    'test_number'=>$v['C'],
                    'class'=>$v['D'],
                    'student_name'=>$v['E'],
                    'math_num'=>$v['F'],
                    'language_num'=>$v['G'],
                    'chines_num'=>$v['H'],
                    'politics_num'=>$v['I'],
                    'history_num'=>$v['J'],
                    'geography_num'=>$v['K'],
                    'biology_num'=>$v['L'],
                    'chemistry_num'=>$v['M'],
                    'physics_num'=>$v['N'],
                    'eligible_subjects'=>$v['O'],
                    'total_points'=>$v['P'],
                    'average'=>$v['Q'],
                    'class_level'=>$v['R'],
                    'school_level'=>$v['S'],
                );
        }

        //p($dat[0]);
        $this->subject=$dat[0];
        $this->display('save_import');
        session('dat',$dat);


    }

    public  function save_imp(){
        //所选的科目
        $subjects=$_POST;
        //全部数据
        $all_dat=$_SESSION['dat'];
        //p($all_dat);
                foreach($all_dat as $k=>$v){
            if($k>=2){
                $dat=array(
                    'student_number'=>$v['student_number'],
                    'test_number'=>$v['test_number'],
                    'class'=>$v['class'],
                    'student_name'=>$v['student_name']
                );
            }
                    M('student_info')->add($dat);
                //p($dat);
        }
        $field=array('id','test_number');
        $note=M('student_info')->field($field)->select();

        foreach($all_dat as $k=>$v){
            foreach($note as $l){
            if($k>=2){
                if($v['test_number']==$l['test_number']){
                    $data=array(
                        'student_number'=>$v['student_number'],
                        'test_number'=>$v['test_number'],
                        'class'=>$v['class'],
                        'student_name'=>$v['student_name']
                    );
                }
            }
            }
            //M('student_info')->add($dat);
            //p($dat);
        }



        //p($subjects);








    }



    public function see(){
        $this->natija= M('result_db')->select();
        $this->display();
    }






}
    ?>